package com.hisense.ovcloud.warehouse.service;

import cn.hutool.core.util.ObjectUtil;
import com.hisense.ovcloud.commons.configs.base.BaseResponse;
import com.hisense.ovcloud.commons.tools.LocalLFUCacheUtil;
import com.hisense.ovcloud.warehouse.Constants;
import com.hisense.ovcloud.warehouse.dao.ProductInfoDao;
import com.hisense.ovcloud.warehouse.dto.common.DomainInfoDto;
import com.hisense.ovcloud.warehouse.dto.req.AuthBatchReq;
import com.hisense.ovcloud.warehouse.dto.req.CreateProductReq;
import com.hisense.ovcloud.warehouse.entity.device.ProductInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.time.Instant;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ProductService {
    private final ProductInfoDao productInfoDao;
    private final LocalLFUCacheUtil<DomainInfoDto> fetureCodeCache;

    public ProductService(ProductInfoDao productInfoDao, @Qualifier("fetureCodeCache") LocalLFUCacheUtil<DomainInfoDto> fetureCodeCache) {
        this.productInfoDao = productInfoDao;
        this.fetureCodeCache = fetureCodeCache;
    }


    public Mono<BaseResponse> createProduct(Mono<CreateProductReq> req) {
        return req.flatMap(r -> Mono.just(BaseResponse.builder().data(productInfoDao.insert(
                ProductInfo.builder()
                        .domain(r.getMajorDomainId())
                        .subDomainId(r.getSubDomainId())
                        .name(r.getName())
                        .type(r.getType())
                        .model(r.getModel())
                        .os(r.getOs())
                        .communication(r.getCommunication())
                        .protocol(r.getProtocol())
                        .transportProtocol(r.getTransportProtocol())
                        .secType(r.getSecType())
                        .licenseMode(r.getLicenseMode())
                        .thirdCloud(r.getThirdCloud())
                        .description(r.getDescription())
                        .category(r.getCategory())
                        .productImageUrl(r.getProductImageUrl())
                        .departmentId(r.getDepartmentId())
                        .manufacture(r.getManufacture())
                        .deviceUplinkUrl(Constants.Product.CANNOT_BE_EMPTY)
                        .createTime(Date.from(Instant.now()))
                        .modifyTime(Date.from(Instant.now()))
                        .featurecode(r.getFeaturecode())
                        .productType(r.getVariety())
                        .brand(r.getBrand())
                        .build()
                )).build())
        );
    }

    public Mono<DomainInfoDto> getDomainInfoWithWifiId(Mono<AuthBatchReq> req) {
        return req.flatMap(r -> Mono.just(fetchDomainInfoByWifiId(r.getWifiId())));
    }

    public DomainInfoDto fetchDomainInfoByWifiId(String wifiId){
        String key = wifiId.substring(0, 24);
        DomainInfoDto dto = fetureCodeCache.getFromCache(key);
        if(ObjectUtil.isNotNull(dto)){
            return dto;
        }else{
            List<DomainInfoDto> infos = productInfoDao.selectDomainInfoByFeatureCode(key);
            if (!infos.isEmpty()) {
                fetureCodeCache.saveToCache(key, infos.getFirst());
                return infos.getFirst();
            }
            return null;
        }
    }
}
